package cn.itcast.goods.admin.book.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.goods.book.domain.Book;
import cn.itcast.goods.book.service.BookService;
import cn.itcast.goods.category.domain.Category;
import cn.itcast.goods.category.service.CategoryService;
import cn.itcast.goods.pager.PageBean;
import cn.mytools.commons.CommonUtils;
import cn.mytools.servlet.BaseServlet;

public class AdminBookServlet extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4327959598736571566L;
	private CategoryService categoryService = new CategoryService();
	private BookService bookService = new BookService();
	
	public String edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Book book = CommonUtils.toBean(req.getParameterMap(), Book.class);
		bookService.edit(book);
		Book editBook = bookService.findByBid(book.getBid());
		req.setAttribute("book", editBook);
		
		Category category = categoryService.findByCid(book.getCid());//查询当前图书所在分类
		req.setAttribute("category", category);
		
		List<Category> parents = categoryService.findAllParents();	//返回所有父分类
		req.setAttribute("parents", parents);
		
		List<Category> children = categoryService.findAllChildren(category.getParent().getCid());	//返回所有二级分类
		req.setAttribute("children", children);
		req.setAttribute("msg", "图书修改成功！");
		return "f:/adminjsps/admin/book/desc.jsp";
	}
	public String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String bid = req.getParameter("bid");
		bookService.delete(bid);
		req.setAttribute("code", "success");
		req.setAttribute("msg", "图书删除成功");
		return "f:/adminjsps/msg.jsp";
	}
	public String addBookPre(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Category> parents = categoryService.findAllParents();	//返回所有父分类
		req.setAttribute("parents", parents);
		return "f:/adminjsps/admin/book/add.jsp";
	}
	
	/**
	 * 查询所有分类
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Category> categoryList = categoryService.findAll();
		req.setAttribute("parents", categoryList);
		return "f:/adminjsps/admin/book/left.jsp";
	}
	/**
	 * 获取当前页的页码
	 * @param req
	 * @return
	 */
	private int getPc(HttpServletRequest req) {
		int pc =1;
		String param = req.getParameter("pc");
		if(param != null && !param.trim().isEmpty()){
			try {
				pc = Integer.parseInt(param);  
			}catch (RuntimeException e) {}  //如果param为abc强转为int会出异常
		}
		return pc;
	}
	/**
	 * 截取url，页面中的分页导航需要使用它做为超链接的目标
	 * @param req
	 * @return
	 */
	private String getUrl(HttpServletRequest req) {
		//等于 /项目名/Servlet路径?method=find&pc=3
		String url = req.getRequestURI()+"?"+req.getQueryString();
		int index = url.lastIndexOf("&pc=");
		if(index != -1) {
			url = url.substring(0,index);
		}
		return url ; 
	}
	/**
	 * 按分类查询
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pc = getPc(req); //得到pagecode当前页码
		String url = getUrl(req);
		
		String cid = req.getParameter("cid");

		PageBean<Book> pb = bookService.findByCategory(cid, pc);
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		
		return "f:/adminjsps/admin/book/list.jsp";
	}
	/**
	 * 按作者查询
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByAuthor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pc = getPc(req); //得到pagecode当前页码
		String url = getUrl(req);
		
		String author = req.getParameter("author");
		
		PageBean<Book> pb = bookService.findByAuthor(author, pc);
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		
		return "f:/adminjsps/admin/book/list.jsp";
	}
	
	/**
	 * 按出版社查询
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByPress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pc = getPc(req); //得到pagecode当前页码
		String url = getUrl(req);
		
		String press = req.getParameter("press");
		
		PageBean<Book> pb = bookService.findByPress(press, pc);
		
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		
		return "f:/adminjsps/admin/book/list.jsp";
	}
	/**
	 * 按bid查询
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByBid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String bid = req.getParameter("bid");
		Book book = bookService.findByBid(bid);
		req.setAttribute("book", book);
		
		Category category = categoryService.findByCid(book.getCid());//查询当前图书所在分类
		req.setAttribute("category", category);
		
		List<Category> parents = categoryService.findAllParents();	//返回所有父分类
		req.setAttribute("parents", parents);
		
		List<Category> children = categoryService.findAllChildren(category.getParent().getCid());	//返回所有二级分类
		req.setAttribute("children", children);
		
		return "f:/adminjsps/admin/book/desc.jsp";
	}
	/**
	 * 多条件组合查询
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByCombination(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pc = getPc(req); //得到pagecode当前页码
		String url = getUrl(req);
		
		Book criteria = CommonUtils.toBean(req.getParameterMap(), Book.class);
		
		PageBean<Book> pb = bookService.findByCombination(criteria, pc);
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		
		return "f:/adminjsps/admin/book/list.jsp";
	}
	/**
	 * ajax获取二级分类
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String ajaxFindChildren(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pid = req.getParameter("pid");
		List<Category> children = categoryService.findAllChildren(pid);
		String json = toJson(children);
		resp.getWriter().print(json);
		return null;
	}
	// {"cid":"fdsafdsa", "cname":"fdsafdas"}
		private String toJson(Category category) {
			StringBuilder sb = new StringBuilder("{");
			sb.append("\"cid\"").append(":").append("\"").append(category.getCid()).append("\"");
			sb.append(",");
			sb.append("\"cname\"").append(":").append("\"").append(category.getCname()).append("\"");
			sb.append("}");
			return sb.toString();
		}
		
		// [{"cid":"fdsafdsa", "cname":"fdsafdas"}, {"cid":"fdsafdsa", "cname":"fdsafdas"}]
		private String toJson(List<Category> categoryList) {
			StringBuilder sb = new StringBuilder("[");
			for(int i = 0; i < categoryList.size(); i++) {
				sb.append(toJson(categoryList.get(i)));
				if(i < categoryList.size() - 1) {
					sb.append(",");
				}
			}
			sb.append("]");
			return sb.toString();
		}
}
